﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

//using DataDefine;
//using WorldDefine;
using HLLib;
//using HLLIB_ASSETBUNDLE;

/// <summary>
/// 启动与下载部分
/// </summary>
public class AppInit : MonoBehaviour
{

	/// <summary>
	/// The is limite frame.
	/// </summary>
	public bool isLimiteFrame;
	public Camera _main_camera;
	public GameObject _uiroot = null;
	public Camera _ui_camera = null;
//	public EasyTouch _easytouch = null;
	GlobalEasyTouchEvent _aaaa = null;

	public static AppInit instance = null;
	public GameObject	_uiloading;
	/// <summary>
	/// Gets the instance.
	/// </summary>
	/// <returns>The instance.</returns>
	public static AppInit getInstance ()
	{
		return instance;
	}


	/// <summary>
	/// Awake this instance.
	/// </summary>ss
	void Awake ()
	{
		instance = this;
		///
//		GameScreen.__uiroot = _uiroot;
//		GameScreen.__main_camera = _main_camera;
//		GameScreen.__Easytouch = _easytouch;
//		GameScreen._ui_camera = _ui_camera;

		///
		if (null == _uiroot || null == _main_camera || null == _ui_camera) {
			Debug.LogError (" AppInit Config Error !!!");
			return;
		}

		/// Add Manager Scripts:
		AddManagerScript ();
	}


	/// <summary>
	/// Start this instance.
	/// </summary>
	void Start ()
	{
		initGameState ();
	}


	/// <summary>
	/// Inits the state of the game.
	/// </summary>
	///	GameScreen.SharedGameScreenManager().descriptionSystemInfo();
	///	DatasManager.getDataInstance().creatAllGameDataTable();
	void initGameState ()
	{	

		/// Sdk Init:
		SdkManagerInit ();

		/// MainLoading UI Create:
		createUICoverDownload ();

		/// Download intenet:
		DownloadInternet ();

		/// Select Resources Load / Select AssertBundle Load ( Test )
		selectResourcesOrAssertBundle ();

		/// ParseAllDataTable:
		ParseAllDataTable ();


		/// startGame:
//		if (HttpDownAssetbundle._godownload == false) {
//			startGame ();
//		}
	}


	/// startGame:



	/// <summary>
	/// Sets the add manager script.
	/// </summary>
	/// <value>The add manager script.</value>
	public void AddManagerScript ()
	{
//		/// AssetBundle Manager:
//		this.gameObject.AddComponent<AssetbundleManager> ();
//		///
	}



	/// <summary>
	/// SdkManagerInit
	/// </summary>
	public void SdkManagerInit ()
	{
		Debug.Log ("SdkManagerInit Sucess !!!");
	}


	/// <summary>
	/// Mains the loading user interface create.
	/// </summary>
	public void createUICoverDownload ()
	{
//		Debug.Log (" ------------------------createUICoverDownload------------------------ ");
//		UIManager um = gameObject.AddComponent<UIManager> ();
//		_uiloading = Instantiate (Resources.Load ("UISystem/UILoading")) as GameObject;
//		if (null != _uiloading) {
//			_uiloading.transform.parent = um.Lowet.transform;
//			_uiloading.transform.localScale = Vector3.one;
//			//	_uiloading.AddComponent<HTTPDOWNASSETBUNDLE>();
//		} else {
//			return;
//		}

	}

	public void DestoryDownload ()
	{
		if(_uiloading.gameObject!=null)
		{
			Destroy (_uiloading.gameObject);
			_uiloading = null;
		}
	}


	/// <summary>
	/// Downloads the internet.
	/// </summary>
	public void DownloadInternet ()
	{
//		DatasManager.getDataInstance ().creatAllGameDataTable ();
	}


	/// <summary>
	/// Selects the resources or assert bundle.
	/// GameScreen.SharedGameScreenManager().loadingProcess (0 ~ 100)
	/// </summary>
	public void selectResourcesOrAssertBundle ()
	{
//		Debug.Log (" ------------------------selectResourcesOrAssertBundle------------------------ ");
//		GameScreen.isOpenAssetBunlde = true; // open assetbudnle
//		Debug.Log (" ------------------------selectResourcesOrAssertBundle------------------------ ");
	}


	/// <summary>
	/// Parses the data table,
	/// </summary>

	public void ParseAllDataTable ()
	{
//		Debug.Log (" ------------------------ParseDataTable------------------------ ");
//		/// Add DataTableClass:
//		DatasManager.getDataInstance ().creatAllGameDataTable ();
//		/// 
//		Debug.Log (" ------------------------ParseDataTable------------------------ ");
	}


	/// <summary>
	/// Starts the game.
	/// </summary>
	public void startGame ()
	{
//		Debug.Log (" -----------------------startGame----------------------------- ");
//		//		AssetbundleManager.instance.LoadAssetbundleBytesWithDatatable("Configdatas/Level001ConfigData");
//		GameScreen.SharedGameScreenManager ().descriptionSystemInfo ();
//		//		GameScreen.SharedGameScreenManager()._GlobalObj = __GlobalObj;
//		DestoryDownload ();
//		Debug.Log (" -----------------------startGame----------------------------- ");
	}

	void Update ()
	{

		//		if(AssetbunderLoad.async!=null)
		//		{
		//			float mk=AssetbundleManager.instance.getSynChnageScenesProcess(AssetbunderLoad.async);
		//			Debug.Log("mk: " + mk);
		//			if(mk == 100)
		//			{
		//				AssetbunderLoad.async.allowSceneActivation = true;
		//				AssetbunderLoad.async=null;
		//			}
		//		}
		//		Debug.Log("AssetbundleManager.loadingSynAsssetBundleNum: " + AssetbundleManager.loadingSynAsssetBundleNum);
	}

	//	public GameObject test_assetBundle = null;
	void OnGUI ()
	{

		/// test:

		//		if(GUI.Button(new Rect(10,10,500,50), "Create HLGameObject With AssetBundle"))
		//		{
		//			HLGameObject hl_obj = new HLGameObject("Modles/1002_fish_jingyu_prefab");
		//			hl_obj.transform.position = new Vector3(1.0f,1.0f,5.0f);
		////			HLGameObject hl_obj2 = new HLGameObject("TestPerfab/","1001_perfab");
		////			hl_obj2.transform.position = new Vector3(0.0f,0.1f,6.0f);
		//		}
		//
		//
		//		if(GUI.Button(new Rect(10,100,500,50), "Change Syn Scene !!!" ))
		//		{
		//			GameScreen.SharedGameScreenManager().changeSubScreen((int )ScreenType.SCREEN_TYPE_COVER);
		//		}


		//		if(GUI.Button(new Rect(10,200,500,50),"LOAD ALL AssetBundle"))
		//		{
		//
		//			AssetbundleManager.instance.AddAssertBundleWithGameObject("DataTable/allDataTable.assetbundle");
		//	
		//		}
		//
		//		if(GUI.Button(new Rect(10,300,500,50),"ass"))
		//		{
		//					Object[] array_obj = AssetbundleManager.instance.ReturnCollective("DataTable/allDataTable");
		//			for(int i=0;i<array_obj.Length;i++)
		//			{
		//				Debug.Log(array_obj[i].name);
		//				TextAsset AAA = (TextAsset)array_obj[i];
		//				Debug.Log(AAA.text);
		//				Debug.Log("--------------------------------------------");
		//			}
		//		}

		//		if(GUILayout.Button("Press AssetBundle Button"))
		//		{
		//			GameObject obj = HLResourcesCacheManager.getInstance().CreateWithAssetBundleGameObject("Modles/1001_perfab");
		//		}
		//
		//
		//		if(GUILayout.Button("Release AssetBundle Button"))
		//		{
		//			AssetbundleManager.instance.ReleaseAssetbundle("Modles/1001_perfab",true);
		//		}
		//
		//		if(GUI.Button(new Rect(10,80,500,50), "Delete HLGameObject With AssetBundle"))
		//		{
		////			AssetbundleManager.instance.DeleteAssetBundleBytesWithCurScene();
		//			HLResourcesCacheManager.getInstance().DeleteAllAssetBundleInfoAndClearGameObjectinCache();
		//		}


		//		if(GUI.Button(new Rect(10,150,200,50), "Change Syn Scene!!! "))
		//		{
		//			AssetbundleManager.instance.AssetAsyncChangeScene("Scenes/ScreenLogo");//AssetAsyncChangeScene
		//		}
		//
		//
		//		if(GUI.Button(new Rect(250,150,200,50), "Change Syn Scene2!!! "))
		//		{
		//			AssetbundleManager.instance.AssetAsyncChangeScene("Scenes/ScreenLogo");
		////			Application.LoadLevel("Test002");
		//		}

		//		if(GUILayout.Button("Press Button"))
		//		{
		////			GameObject obj = GameObject.Instantiate(AssetbundleManager.instance.CreateAssetBundleGameObject("Modles/Weapon01_AK_prefab")) as GameObject;
		////			GameObject obj2 = GameObject.Instantiate(AssetbundleManager.instance.CreateAssetBundleGameObject("Modles/1001_perfab")) as GameObject;
		//		}


		//		if(GUILayout.Button("Press Scene Button !!!"))
		//		{
		//			AssetbundleManager.instance.AssetAsyncChangeScene("Scenes/Test001");
		//		}

	}

}
